package com.example.oasystem.controller.role;


import com.example.oasystem.dao.login.AoaUserDao;
import com.example.oasystem.entity.list.AoaSysMenu;
import com.example.oasystem.entity.page.Page;
import com.example.oasystem.entity.role.AoaRole;
import com.example.oasystem.entity.role.AoaRolePowerList;
import com.example.oasystem.entity.user.AoaUser;
import com.example.oasystem.service.AoaRoleService;
import com.example.oasystem.service.AoaUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class RolePowerMenuController {

    @Autowired
    private AoaRoleService aoaRoleService;

    @Autowired
    private AoaUserService aoaUserService;

    @RequestMapping("/rolemanage")
    public String quryRole(@RequestParam(value = "page",required = false,defaultValue = "0") int page,@RequestParam(value = "baseKey",required = false,defaultValue = "") String roleName, Model model){
        List<AoaRole> aoaRoles = aoaRoleService.selectRole(null,roleName);
        int size=6;
        int totalPages=aoaRoles.size()%size==0?aoaRoles.size()/size:aoaRoles.size()/size+1;
        Page p=new Page(aoaRoles.size(),size,totalPages,page*size,true,false);
        System.out.println("p 11111111111111111111111111111111111= " + p);
        List<AoaRole> aoaRolelist = aoaRoleService.selectRole(p,roleName);
        p=new Page(aoaRoles.size(),size,totalPages,page,true,false);
        System.out.println("p 2222222222222222222222222222222222= " + p);
        if (totalPages==1){
            p.setFirst(true);
            p.setLast(true);
        }else if (page<=0){
            p.setFirst(true);
            p.setLast(false);
        }else
        if (page>=totalPages-1){
            p.setFirst(false);
            p.setLast(true);
        }else {
            p.setFirst(false);
            p.setLast(false);
        }

        model.addAttribute("rolelist",aoaRolelist);
        model.addAttribute("page",p);
        model.addAttribute("url","rolepaging");
        return "role/rolemanage";
    }

    @RequestMapping("/rolepaging")
    public String paging(@RequestParam(value = "page",required = false,defaultValue = "0") int page,@RequestParam(value = "baseKey",required = false,defaultValue = "") String roleName, Model model){
        List<AoaRole> aoaRoles = aoaRoleService.selectRole(null,roleName);
        int size=6;
        int totalPages=aoaRoles.size()%size==0?aoaRoles.size()/size:aoaRoles.size()/size+1;
        Page p=new Page(aoaRoles.size(),size,totalPages,page*size,true,false);
        System.out.println("p1111111111111111111111111111111111111111= " + p);
        List<AoaRole> aoaRolelist = aoaRoleService.selectRole(p,roleName);
        p=new Page(aoaRoles.size(),size,totalPages,page,true,false);
        if (totalPages==1){
            p.setFirst(true);
            p.setLast(true);
        }else if (page<=0){
            p.setFirst(true);
            p.setLast(false);
        }else
        if (page>=totalPages-1){
            p.setFirst(false);
            p.setLast(true);
        }else {
            p.setFirst(false);
            p.setLast(false);
        }
        System.out.println("ppppppppp222222222222222222222222222222222222 = " + p);
        model.addAttribute("rolelist",aoaRolelist);
        model.addAttribute("page",p);
        model.addAttribute("url","rolepaging");
        return "role/roletable";
    }

    @RequestMapping("/roleset")
    public String roleset(@RequestParam("id")long id,Model model){
        AoaRole aoaRole = aoaRoleService.selectOneRole(id);
        List<AoaSysMenu> oneSysMenus = aoaRoleService.selectPowerMenu(id);
        List<AoaSysMenu> twoSysMenus = aoaRoleService.selectPowerMenu(id);
        model.addAttribute("oneMenuAll",oneSysMenus);
        model.addAttribute("twoMenuAll",twoSysMenus);
        model.addAttribute("role",aoaRole);
        return "role/roleset";
    }

//    @RequestMapping("/menupaging")
//    public String menupaging(@RequestParam("id")long id,Model model){
//        AoaRole aoaRole = aoaRoleService.selectOneRole(id);
//        List<AoaSysMenu> oneSysMenus = aoaRoleService.selectRoleMenu(0,null);
//
//        List<AoaSysMenu> twoSysMenus = aoaRoleService.selectRoleMenu(id,null);
//        model.addAttribute("oneMenuAll",oneSysMenus);
//        model.addAttribute("twoMenuAll",twoSysMenus);
//        model.addAttribute("role",aoaRole);
//        return "role/rolesetable";
//    }

    @RequestMapping("/addrole")
    public String addrole(@RequestParam(value = "id",required = false,defaultValue = "0")int id,Model model){
        if (id==0){
            return "role/addrole";
        }else {
            AoaRole aoaRole = aoaRoleService.selectOneRole(id);
            model.addAttribute("role",aoaRole);
            return "role/addrole";
        }
    }

@RequestMapping("/modifyrole")
    public String modifyrole(AoaRole aoaRole,Model model){
        if (aoaRole.getRoleId()!=null && aoaRole.getRoleId()!=0){
            int i = aoaRoleService.updateRole(aoaRole);
            if (i<=0){
                model.addAttribute("errormess","用户名已重复，请重新填写");
                model.addAttribute("role",aoaRole);
                return "forward:/addrole";
            }

        }else {
            List<AoaSysMenu> list = aoaRoleService.selectRoleMenu(0, null);
            int i = aoaRoleService.addRole(aoaRole, list);
            if (i<=0){
                model.addAttribute("errormess","用户名已重复，请重新填写");
                model.addAttribute("role",aoaRole);
                return "forward:/addrole";
            }
        }
        return "forward:/rolemanage";
    };


    @RequestMapping("/deshan")
    public String delectRole(@RequestParam("id")long roleId, @RequestParam("page")int page, HttpServletRequest request,Model model){
        List<AoaUser> user = aoaUserService.getUser(roleId);
        if (user.size()<=0){
            aoaRoleService.delRole(roleId);
            request.setAttribute("page",page);
            return "forward:/rolepaging";
        }else {
            model.addAttribute("errormess","还无法删除该角色");
            return "forward:/rolepaging";
        }

    }


    @RequestMapping("/powerss")
    public void updateRolePower(HttpServletRequest request, HttpServletResponse response){
        System.out.println("权限修改中");
        String isShow = request.getParameter("isShow");
        String menuId = request.getParameter("menuId");
        String roleId = request.getParameter("roleId");
        System.out.println("roleId 1111111111111111111111111= " + roleId);
        System.out.println("isShow 11111111111111111111111= " + isShow);
        System.out.println("menuId 111111111111111111111= " + menuId);
        int linNumber = aoaRoleService.updateRolePower(Integer.parseInt(isShow),Long.parseLong(menuId),Long.parseLong(roleId));
        try {
            response.getWriter().write(linNumber);
        } catch (IOException e) {
            e.printStackTrace();
        }
//        return "forward:/roleset";
    }
}
